package com.dt.dubai.mber.controller;

import com.alibaba.fastjson.JSON;
import com.dt.dubai.core.exception.PageErrorMesg;
import com.dt.dubai.mber.entity.MberCustomerOrderEntity;
import com.dt.dubai.mber.controller.innate.MberCustomerOrderInnateAbs;
import com.dt.dubai.core.comm.*;
import com.dt.dubai.core.utile.DateTools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/*
* 用户商品订单表
*/

@Controller
@RequestMapping(value = "/mber/mberCustomerOrder")
public class MberCustomerOrderController extends MberCustomerOrderInnateAbs {

    /*innateServiceStart*/
    @Override
    protected void userSavEntity(MberCustomerOrderEntity inEntity, final Long userId) {
        inEntity.setDelSign(false);
    }

    @Override
    protected void userModEntity(MberCustomerOrderEntity inEntity, final Long userId) {
    }

    @Override
    protected void userDelByKey(List<Long> recordIds, final Long userId) {
    }

    @Override
    protected void userQueByKey(Long recordId, Map<String, Object> map, final Long userId) {
        if (null != map && map.size() > 0) {
            map.put("createTxStamp", DateTools.dateFormat((Date) map.get("createTxStamp"), DateTools.DATE_FORMAT_YMD));
            map.remove("customerRecordId");
            map.remove("customerAddrRecordId");
            map.remove("delSign");
            map.remove("lastupTxStamp");
        }
    }

    @Override
    protected void userQuePageList(PageParameterData queryCondition, List<PageCondition> joinCondition, List<Map<String, Object>> resultMap, final Long userId) {
        for (Map<String, Object> map : resultMap) {
            map.put("createTxStamp", DateTools.dateFormat((Date) map.get("createTxStamp"), DateTools.DATE_FORMAT_YMD));
            Short payType = null == map.get("payType") ? -1 :  (Short) map.get("payType");
            switch (payType) {
                case 1:
                    map.put("payType", payType);
                    map.put("payTypeStr", "支付宝");
                    break;
                case 2:
                    map.put("payType", payType);
                    map.put("payTypeStr", "微信");
                    break;
                case 3:
                    map.put("payType", payType);
                    map.put("payTypeStr", "其它");
                    break;
                default:
                    map.put("payType", -1);
                    map.put("payTypeStr", "未知");
            }
            Short deliverGoods = null == map.get("deliverGoods") ? -1 :  (Short) map.get("deliverGoods");
            switch (deliverGoods){
                case 1:
                    map.put("deliverGoods", deliverGoods);
                    map.put("deliverGoodsStr", "未发货");
                    break;
                case 2:
                    map.put("deliverGoods", deliverGoods);
                    map.put("deliverGoodsStr", "已发货");
                    break;
                case 3:
                    map.put("deliverGoods", deliverGoods);
                    map.put("deliverGoodsStr", "派送中");
                    break;
                case 4:
                    map.put("deliverGoods", deliverGoods);
                    map.put("deliverGoodsStr", "已签收");
                    break;
                case 5:
                    map.put("deliverGoods", deliverGoods);
                    map.put("deliverGoodsStr", "已完成");
                    break;
                default:
                    map.put("commodityType", -1);
                    map.put("commodityTypeStr", "未知");
            }

            Short commodityType = null == map.get("commodityType") ? -1 :  (Short) map.get("commodityType");
            switch (commodityType) {
                case 1:
                    map.put("commodityType", commodityType);
                    map.put("commodityTypeStr", "电商");
                    break;
                case 2:
                    map.put("commodityType", commodityType);
                    map.put("commodityTypeStr", "检测");
                    break;
                case 3:
                    map.put("commodityType", commodityType);
                    map.put("commodityTypeStr", "计量");
                    break;
                default:
                    map.put("commodityType", -1);
                    map.put("commodityTypeStr", "未知");
            }
            Short orderStatus = null == map.get("orderStatus") ? -1 :  (Short) map.get("orderStatus");
            switch (orderStatus) {
                case 1:
                    map.put("orderStatus", orderStatus);
                    map.put("orderStatusStr", "未支付");
                    break;
                case 2:
                    map.put("orderStatus", orderStatus);
                    map.put("orderStatusStr", "已支付");
                    break;
                case 3:
                    map.put("orderStatus", orderStatus);
                    map.put("orderStatusStr", "已取消");
                    break;
                default:
                    map.put("orderStatus", -1);
                    map.put("orderStatusStr", "未知");
            }
            Short logisticsFirm = null == map.get("logisticsFirm") ? -1 :  (Short) map.get("logisticsFirm");
            switch (logisticsFirm) {
                case 1:
                    map.put("logisticsFirm", logisticsFirm);
                    map.put("logisticsFirmStr", "中通");
                    break;
                case 2:
                    map.put("logisticsFirm", logisticsFirm);
                    map.put("logisticsFirmStr", "顺丰");
                    break;
                case 3:
                    map.put("logisticsFirm", logisticsFirm);
                    map.put("logisticsFirmStr", "圆通");
                    break;
                case 4:
                    map.put("logisticsFirm", logisticsFirm);
                    map.put("logisticsFirmStr", "EMS");
                    break;
                default:
                    map.put("logisticsFirm", -1);
                    map.put("logisticsFirmStr", "未知");
            }
            Short afterSalesStatus = null == map.get("afterSalesStatus") ? -1 :  (Short) map.get("afterSalesStatus");
            switch (afterSalesStatus) {
                case 1:
                    map.put("afterSalesStatus", afterSalesStatus);
                    map.put("afterSalesStatusStr", "无");
                    break;
                case 2:
                    map.put("afterSalesStatus", afterSalesStatus);
                    map.put("afterSalesStatusStr", "申请退货");
                    break;
                case 3:
                    map.put("afterSalesStatus", afterSalesStatus);
                    map.put("afterSalesStatusStr", "已退货");
                    break;
                case 4:
                    map.put("afterSalesStatus", afterSalesStatus);
                    map.put("afterSalesStatusStr", "申请换货");
                    break;
                case 5:
                    map.put("afterSalesStatus", afterSalesStatus);
                    map.put("afterSalesStatusStr", "已换货");
                    break;
                case 6:
                    map.put("afterSalesStatus", afterSalesStatus);
                    map.put("afterSalesStatusStr", "赔付");
                    break;
                case 7:
                    map.put("afterSalesStatus", afterSalesStatus);
                    map.put("afterSalesStatusStr", "同意");
                    break;
                case 8:
                    map.put("afterSalesStatus", afterSalesStatus);
                    map.put("afterSalesStatusStr", "拒绝");
                    break;
                default:
                    map.put("afterSalesStatus", -1);
                    map.put("afterSalesStatusStr", "未知");
            }
            map.remove("delSign");
            map.remove("lastupTxStamp");
        }
    }

    /*innateServiceEnd*/
    /*以上内容请误删除或是移动，系统升级使用，用户代码请写入下方*/

    //用户申请退换货
    @RequestMapping(value = "/applyReturnGoods", method = RequestMethod.POST)
    @ResponseBody
    public PageData<String> applyReturnGoods(@RequestParam("data")  String data, final Long userId){
        try {
            SqlUtils.clearSqlCode(data);
            SqlUtils.clearWebCode(data);
            MberCustomerOrderEntity entity = JSON.parseObject(data, MberCustomerOrderEntity.class);
            userModEntity(entity, userId);
            mberCustomerOrderSer.applyReturnGoods(entity, userId);
            return new PageData().success("", i18nUtile.getMessage(PageErrorMesg.E0.name()));
        } catch (RuntimeException e) {
            String code = e.getMessage() == null ? "E911" : e.getMessage().startsWith("E") ? e.getMessage() : "E911";
            System.out.println(i18nUtile.getMessage(code));
            e.printStackTrace();
            return PageData.error(code, i18nUtile.getMessage(code));
        }

    }

    //发货
    @RequestMapping(value = "/deliverGoods", method = RequestMethod.POST)
    @ResponseBody
    public PageData<String> deliverGoods(@RequestParam("data")  String data, final Long userId){
        try {
            SqlUtils.clearSqlCode(data);
            SqlUtils.clearWebCode(data);
            MberCustomerOrderEntity entity = JSON.parseObject(data, MberCustomerOrderEntity.class);
            userModEntity(entity, userId);
            mberCustomerOrderSer.applyReturnGoods(entity, userId);
            return new PageData().success("", i18nUtile.getMessage(PageErrorMesg.E0.name()));
        } catch (RuntimeException e) {
            String code = e.getMessage() == null ? "E911" : e.getMessage().startsWith("E") ? e.getMessage() : "E911";
            System.out.println(i18nUtile.getMessage(code));
            e.printStackTrace();
            return PageData.error(code, i18nUtile.getMessage(code));
        }

    }
}
